package com.example.kafkademo.producer;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.util.Properties;

/**
 * @author: HuangSiBo
 * @Description:异步发送消息
 * @Data: Created in 10:43 2021/12/26
 */
public class MyProducer {

    public static void main(String[] args) {
        Properties properties = new Properties();
        //连接的集群，这些参数的含义可以点进去看详细信息
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"110.42.176.63:9092");
        //key，value的序列化
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");

        //创建生产者
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(properties);

        for (int i = 0; i < 100; i++) {
            kafkaProducer.send( new ProducerRecord<>("first", Integer.toString(i), Integer.toString(i)));
        }

        //关闭生产者
        kafkaProducer.close();
    }
}
